#include "../../../head.h"

typedef struct file_time
{
    int tm_sec;
    int tm_min;
    int tm_hour;
    int tm_mday;
    int tm_mon;
    int tm_year;
} file_time;

int main(int argc, char const *argv[])
{
    if (argc != 2)
    {
        printf("格式错误! ./%s file", argv[0]);
        return 0;
    }

    time_t current_time;
    time(&current_time);
    struct tm *gm_time = gmtime(&current_time);

    file_time f_tm[2];
    memset(f_tm, 0, sizeof(f_tm)); // 初始化结构体

    FILE *fp = fopen(argv[1], "w+");

    f_tm[0].tm_year = gm_time->tm_year + 1900;
    f_tm[0].tm_mon = gm_time->tm_mon;
    f_tm[0].tm_mday = gm_time->tm_mday;
    f_tm[0].tm_hour = gm_time->tm_hour;
    f_tm[0].tm_min = gm_time->tm_min;
    f_tm[0].tm_sec = gm_time->tm_sec;

    fprintf(fp, "%d年 %d月 %d日 %d时:%d分:%d秒\n", f_tm[0].tm_year, f_tm[0].tm_mon,
            f_tm[0].tm_mday, f_tm[0].tm_hour, f_tm[0].tm_min, f_tm[0].tm_sec);

    while (1)
    {
        fp = fopen(argv[1], "a+");

        time(&current_time);
        gm_time = gmtime(&current_time);
        f_tm[1].tm_year = gm_time->tm_year + 1900;
        f_tm[1].tm_mon = gm_time->tm_mon;
        f_tm[1].tm_mday = gm_time->tm_mday;
        f_tm[1].tm_hour = gm_time->tm_hour;
        f_tm[1].tm_min = gm_time->tm_min;
        f_tm[1].tm_sec = gm_time->tm_sec;

        fseek(fp , 0 , SEEK_SET);
        if (f_tm[1].tm_sec != f_tm[0].tm_sec)
        {
            fprintf(fp, "%d年 %d月 %d日 %d时:%d分:%d秒\n", f_tm[1].tm_year, f_tm[1].tm_mon,
                    f_tm[1].tm_mday, f_tm[1].tm_hour, f_tm[1].tm_min, f_tm[1].tm_sec);
            f_tm[0] = f_tm[1];
        }
        // usleep(1000000);
        fclose(fp);
    }

    

    return 0;
}
